<template>
  <div class="login_container">
    <el-form>
      <el-row>
        <el-col :span="12" :xs="0"></el-col>
        <el-col :span="12" :xs="24">
          <el-form class="login_form" :module="loginForm" :rules="rules" ref="loginForms">
            <h1>hello</h1>
            <h2>欢迎来到硅谷甄选</h2>
            <el-form-item prop="username">
              <el-input placeholder="账号" :prefix-icon="User" v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input placeholder="密码" type="password" :prefix-icon="Lock" show-password
                v-model="loginForm.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button :loading="loading" @click="login" class="login_btn" type="primary"
                size="default">登录</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue';
// 引入用户相关的小仓库
import useUserStore from '@/store/modules/user';
import { useRouter, useRoute } from 'vue-router';
import { ElNotification } from 'element-plus';
// 获取当前时间函数
import { getTime } from '@/utils/time';

//获取el-form
let loginForms = ref()
let useStore = useUserStore()
// 获取路由
let $router = useRouter()
let $route = useRoute()
// 定义变量控制按钮加载效果
let loading = ref(false)
// 收集账号与密码的数据
let loginForm = reactive({
  username: 'admin',
  password: '111111'
})
// 登录按钮回调
const login = async () => {
  // 保证全部的表单项校验通过发请求
  await loginForms.value.validate()

  // 加载效果:开始加载
  loading.value = true
  // 点击登录按钮干什么
  // 通知仓库发请求
  // 请求成功->首页展示数据
  // 请求失败->弹出登录失败信息
  try {
    // 保证登录成功 
    await useStore.userLogin(loginForm)
    // 编程式导航
    // 判断登录时,路由路径是否有query参数,如果有跳转query,如果没有跳转到首页展示数据
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })

    // 登录成功提示信息
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `Hi,${getTime()}好`
    })
    loading.value = false
  } catch (error) {
    loading.value = false
    // 登录失败提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message
    })
  }

}

// 自定义校验规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  console.log(loginForm.username);

  if (loginForm.username.length >= 5) {
    callback()
  } else {
    return callback(new Error('账号长度至少五位'))
  }
}

const validatorUserPassword = (rule: any, value: any, callback: any) => {
  if (loginForm.password.length >= 6) {
    callback()
  } else {
    return callback(new Error('密码长度至少六位'))
  }
}

// 定义表单校验的回调
const rules = {
  username: [
    { trigger: 'change', validator: validatorUserName }
  ],
  password: [
    { trigger: 'change', validator: validatorUserPassword }
  ]
}

</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: #fff;
      font-size: 40px;
    }

    h2 {
      color: #fff;
      font-size: 20px;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>